from Individual import *
from random import *

class SexualReproduction:
    def __init__(self,mutationRate=0.01):
        self.mutationRate=mutationRate
        return
        
    def mate(self,pair):
        (child1,child2) = self.crossoverSingle(pair)
        if (self.mutationRate > 0):
            mutatedChild1 = self.mutate(child1)
            mutatedChild2 = self.mutate(child2)
        return (mutatedChild1,mutatedChild2)
    
    def mutate(self,child):
        if (random() > mutationRate):
            return child # return child unmutated
        else:
            child.invert( randint(0,child.length()) )
            return child
        
    
    def crossoverSingle(self,pair,locOfCross=0.5,range=0.5):
        (i1,i2) = pair
        l1 = len(i1.genome)
        l2 = len(i2.genome)
        
        genome3 = i1.genome[0:int(l2*locOfCross)] + i2.genome[int(l2*locOfCross):]
        genome4 = i2.genome[0:int(l2*locOfCross)] + i1.genome[int(l2*locOfCross):]
        
        i3 = Individual(genome3, max(i1.generation,i2.generation)+1)
        i4 = Individual(genome4, i3.generation)
        return (i3,i4)
        
    def crossoverDouble(self,pair,loc1=0.33,loc2=0.67,probOfLoc=0.5):
        return pair
    
    